package com.sright.bcard.mini.web.controller;

import com.sright.bcard.common.service.share.dto.request.CardTemplateRequest;
import com.sright.bcard.common.service.share.enums.CardTypeEnum;
import com.sright.bcard.content.share.service.TemplateService;
import com.sright.bcard.common.service.share.dto.enterprise.MaterialDTO;
import com.sright.bcard.enterprise.share.service.MaterialService;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 个人名片controller
 */
@RestController
public class CardPersonalController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private TemplateService templateService;

    /**
     * 获取个人名片素材列表
     * @return
     */
    @GetMapping(WebUrl.PersonalCardMaterial)
    public SrightResponse cardMaterialList(@RequestAttribute TokenParam tokenParam) {
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setUserId(tokenParam.getPersonalUserId());
        materialDTO.setMemberId(tokenParam.getMemberId());
        materialDTO.setCardType(CardTypeEnum.PERSONAL.getValue());
        return materialService.personalMaterialList(materialDTO);
    }

    /**
     * 新增个人名片素材
     * @return
     */
    @PostMapping(WebUrl.PersonalCardMaterial)
    public SrightResponse addCardMaterialList(@RequestBody MaterialDTO materialDTO,@RequestAttribute TokenParam tokenParam) {
        buildMaterialContext(materialDTO,tokenParam);
        materialDTO.setCardType(CardTypeEnum.PERSONAL.getValue());
        return materialService.addPersonalMaterial(materialDTO);
    }

    /**
     * 修改个人名片使用的素材
     * @return
     */
    @PutMapping(WebUrl.PersonalCardMaterial)
    public SrightResponse editCardMaterial(@RequestBody MaterialDTO materialDTO, @RequestAttribute TokenParam tokenParam) {
        return materialService.editPersonalMaterial(materialDTO.getIds(),tokenParam.getPersonalUserId());
    }

    /**
     * 删除个人名片素材
     * @return
     */
    @DeleteMapping(WebUrl.PersonalCardMaterial)
    public SrightResponse deleteCardMaterial(@RequestBody MaterialDTO materialDTO) {
        return materialService.deletePersonalMaterial(materialDTO.getIds());
    }

    /**
     * 个人素材列表
     * @param tokenParam
     * @return
     */
    @GetMapping(WebUrl.PersonalTemplate)
    public SrightResponse templateList(@RequestAttribute TokenParam tokenParam) {
        return templateService.templateList(tokenParam);
    }

    /**
     * 编辑名片模板
     * @param cardTemplateRequest
     * @return
     */
    @PostMapping(WebUrl.PersonalEditUseTemplate)
    public SrightResponse selectTemplate(@RequestBody CardTemplateRequest cardTemplateRequest) {
        return templateService.selectTemplate(cardTemplateRequest.getCardId(), cardTemplateRequest.getTemplateId());
    }

    private MaterialDTO buildMaterialContext(MaterialDTO materialDTO, TokenParam tokenParam) {
        materialDTO.setUserId(tokenParam.getPersonalUserId());
        materialDTO.setMemberId(tokenParam.getMemberId());
        return materialDTO;
    }

}
